﻿using Microsoft.AspNetCore.Mvc;
using NET6.WebAPI.Extensions;
using NET6.WebAPI.Helpers;
using NET6.WebAPI.Models.Word;
using System.Data;

namespace NET6.WebAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AsposeController : ControllerBase
    {
        [HttpPost]
        public IActionResult GenerateExcel()
        {
            var titles = new string[] { "编号", "名字", "多大了" };

            DataTable dt = new DataTable();
            dt.Columns.Add("姓名");
            dt.Columns.Add("年龄", typeof(int));

            DataRow row = dt.NewRow();
            row["姓名"] = "你大爷"; 
            row["年龄"] = 66;
            dt.Rows.Add(row);

            var newDt = dt.AddSequenceColumn();

            string path = FileHelper.GetRootPath();
            string filePath = Path.Combine(path, "output.xlsx");

            AsposeExcelHelper.GenerateExcelByDataTable(newDt, filePath, titles);

            return Ok(filePath);
        }

        [HttpPost]
        public IActionResult GenerateWord()
        {
            var model = new Template
            {
                Today = DateTime.Now.ToString(DateTimeHelper._dateChina),
                Number = "12345",
                CompanyName = "Example Company",
                AgentName = "John Doe",
                AgentTel = "123-456-7890",
                Addr = "123 Main St, Anytown, USA",
                Length = 10.0,
                Width = 5.0,
                Area = 100.0,  
                Size = 20.0,
                Height = 2.0,
                Count = 5, 
                Content = "This is a sample content.",
                StartTime = "09:00", 
                EndTime = "17:00" 
            };

            string path = FileHelper.GetRootPath();
            string templatePath = Path.Combine(path, "Templates/template.docx");
            string filePath = Path.Combine(path, "output.docx");

            AsposeWordHelper.GenerateWordByTemplate(model, templatePath, filePath);

            return Ok(filePath);
        }

        [HttpPost]
        public IActionResult GenerateImage()
        {
            string path = FileHelper.GetRootPath();
            string filePath = Path.Combine(path, "output.pdf");

            AsposePdfHelper.FirstPageToImage(filePath);

            return Ok(filePath);
        }
    }
}
